class String
  def sanitize(length = 64, do_not_simplify = false)
    return if self.blank?
    ret = self.gsub(/<[^>]+>/m,'') #tagstriping (terribly)
    ret = ret.tr('ÁÉÍÓÚáéíóúÄËÏÖÜäëïöüÀÈÌÒÙàèìòùÑñÇçºª_','aeiouaeiouaeiouaeiouaeiouaeiounnssoa-').downcase
    if ret.length > length  
    	if maxlength = ret.rindex(/[^a-z]/, length)
    		ret = ret[0..(maxlength - 1)] 
    	else
    		ret = ret[0..length] 
    	end
    end
    #eliminamos del título aquellas palabras que no son relevantes para los buscadores
    ret.gsub!(/\sde\s|\sa\s|\sque\s|\sno\s|\stiene\s|\sen\s|\spara\s|\spor\s|\sle\s|\sla\s|\slo\s|\slas\s|\slos\s|\sel\s|\suna\s|\sun\s/," ") unless do_not_simplify
    ret.gsub(/&.+?;/,'-').gsub(/[^a-z0-9 _-]/,'-').strip.gsub(/\s+/,'-').gsub(/-+/,'-')
  end

  def desanitize
    return if self.blank?
    return self.split('-').join(' ')
  end
  
  def to_hash(seperation='&', assignment='=')
    hash = Hash.new
    self.split(seperation).each do |elemement|
      pieces = elemement.split(assignment)
      hash[pieces[0]] = pieces[1]
    end
    hash.delete_if { |key, value| value.nil? }
  end
  
end